﻿using System;
using System.Collections.Generic;
using System.Linq;
using ComicQ.Domain.Table;
using NHibernate.Linq;

namespace ComicQ.Controller
{
    public class YearController
    {
        public IList<Year> GetAllYear()
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                return session.Query<Year>().ToList();
            }
        }

        public IList<int> GetAllYearNumber()
        {
            return GetAllYear().Select(year => year.Number).ToList();
        }

        public int GetYearNumber(Year year)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                return session.Query<Year>().ToList().
                    Where(y => y.Id == year.Id).
                    Select(y => y.Number).
                    Single();
            }
        }

        public int AddYear(Year year)
        {
            try
            {
                GetAllYear()
                    .Where(y => y.Number == year.Number)
                    .Select(y => y)
                    .Single();

                return 0;
            }
            catch (Exception)
            {
                using (var session = NHibernateHelper.OpenSession())
                {
                    using (var transaction = session.BeginTransaction())
                    {
                        session.Save(year);
                        transaction.Commit();
                    }
                }

                return 1;
            }
        }

        public int EditYear(Year newYear, int oldNumber)
        {
            try
            {
                var checkYear = GetAllYear()
                    .Where(y => y.Number == newYear.Number)
                    .Select(y => y)
                    .Single();

                if (checkYear.Number == oldNumber)
                {
                    using (var session = NHibernateHelper.OpenSession())
                    {
                        using (var transaction = session.BeginTransaction())
                        {
                            session.SaveOrUpdate(newYear);
                            transaction.Commit();
                        }
                    }

                    return 1;
                }

                return 0;
            }
            catch (Exception)
            {
                using (var session = NHibernateHelper.OpenSession())
                {
                    using (var transaction = session.BeginTransaction())
                    {
                        session.SaveOrUpdate(newYear);
                        transaction.Commit();
                    }
                }

                return 1;
            }
        }

        public void DeleteYear(Year year)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    session.Delete(year);
                    transaction.Commit();
                }
            }
        }
    }
}
